<template>
  <div class="app-content-wrap" :style="'minHeight:' + gHeight + 'px;'">
    <div class="g-content-title">
      楼栋管理
      <div class="department-top-btn">
        <el-button class="btn-default" type="primary" plain icon="el-icon-plus" size="mini" @click="addBuilding()">新增</el-button>
      </div>
    </div>
    <!-- 搜索框 -->
    <el-form :model="houseForm" label-width="80px" size="small">
      <el-row :gutter="15">
        <el-col :span="5">
          <el-form-item label="楼栋编号">
            <el-input v-model="houseForm.buildingNo" placeholder="楼栋编号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="所属小区">
            <AreaSelect :selectModel="houseForm.deptId" :areaIds="areaIds" @onAreaChange="onAreaChange" hideThird="true">
            </AreaSelect>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item>
            <el-button type="primary" size="small" icon="el-icon-search" @click="houseSearch">搜 索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 列表 -->
    <div style="margin:10px 30px;">
      <el-table :data="userListTable" border style="width: 100%">
        <!-- <el-table-column prop="buildingCode" label="楼栋编号" min-width="100" align="center"></el-table-column> -->
        <el-table-column prop="buildingNo" label="楼栋编号" min-width="120" align="center"></el-table-column>
        <el-table-column prop="floorTotal" label="楼层数" min-width="140" align="center"></el-table-column>
        <el-table-column prop="houseTotal" label="户数" min-width="140" align="center"></el-table-column>
        <el-table-column prop="deptName" label="所属小区" min-width="140" align="center"></el-table-column>
        <el-table-column prop="note" label="备注" min-width="140" align="center"></el-table-column>
        <el-table-column label="操作" width="190" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="buildingEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="buildingDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination background @current-change="handleCurrentChange" :current-page.sync="pagination.currentPage" :page-size="10" layout="total, prev, pager, next,jumper" :total="pagination.total" class="el-page-body"></el-pagination>
    </div>
  </div>
</template>
<script>
import AreaSelect from '../../components/common/select/area.vue'

export default {
  name: 'buildingList',
  components: {
    AreaSelect
  },
  data () {
    return {
      gHeight: this.$store.state.gHeight,
      userListTable: [],
      houseForm: {
        buildingNo: '',
        deptId: ''
      },
      areaIds: {
        areaId1: '',
        areaId2: '',
        areaId3: ''
      },
      pagination: {
        currentPage: 1,
        total: null
      }
    }
  },
  created () {
  },
  methods: {
    addBuilding () {
      this.$router.push({
        name: 'addModifyBuilding',
        params: {
          id: 'addBuilding'
        }
      })
    },
    buildingEdit (row) {
      this.$router.push({
        name: 'addModifyBuilding',
        params: {
          id: row.id
        }
      })
    },
    buildingDelete (row) {
      const id = row.id
      this.$confirm('删除该楼栋?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = this.$base.getUrl('bizbuildinfoDelete')
        let params = {
          isJson: true,
          url: url,
          data: {
            id: id
          }
        }
        this.$store.dispatch('postForm', params).then((res) => {
          if (res.code === 0) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getBuildingList()
          }
        })
      }).catch(() => {
        this.$message({
          message: '已取消删除',
          type: 'info'
        })
      })
    },
    handleCurrentChange (val) {
      this.pagination.currentPage = val
      this.getBuildingList()
    },
    getBuildingList () {
      const url = this.$base.getUrl('bizbuildinfoList')
      let params = {
        isJson: true,
        url: url,
        data: {
          buildingNo: this.houseForm.buildingNo,
          deptId: this.houseForm.deptId,
          page: this.pagination.currentPage,
          limit: 10
        }
      }
      this.$store.dispatch('postForm', params).then((res) => {
        let { result } = res
        this.userListTable = result.list
        this.pagination.currentPage = result.currPage
        this.pagination.total = result.totalCount
      })
    },
    houseSearch () {
      this.pagination.currentPage = 1
      this.getBuildingList()
    },
    onAreaChange (val) {
      this.houseForm.deptId = val
    }
  },
  mounted () {
    this.getBuildingList()
  }
}
</script>
<style scoped>
.g-content-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.department-top-btn {
  float: right;
}

.department-top-btn .btn-default {
  display: inline-block;
  padding: 2px 12px;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.42857143;
  text-align: center;
  cursor: pointer;
  background: none;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-right: 30px;
}
.el-table {
  width: auto !important;
  text-align: center;
  margin: 10px;
}

.el-table >>> th {
  text-align: center;
}
</style>
